#include("../../layout/header.html")
#@layout()
#define content()
<head>
  <link rel="stylesheet" href="/static/libs/highlight/default.min.css">
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 表格工具栏 -->
      <form class="layui-form toolbar">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">关键字:</label>
            <div class="layui-input-inline">
              <input name="search" class="layui-input" placeholder="输入表名、表备注"/>
            </div>
          </div>

          <div class="layui-inline">&emsp;
            <button class="layui-btn icon-btn" lay-filter="dataTbSearch" lay-submit>
              <i class="layui-icon">&#xe615;</i>搜索
            </button>

          </div>
        </div>
      </form>
      <!-- 数据表格 -->
      <table id="dataTable" lay-filter="dataTable"></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="dataTbBar">
  <a class="layui-btn layui-btn-xs" lay-event="preview"><i class="layui-icon layui-icon-search"></i>预览</a>
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
<!--  <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">同步</a>-->
  <a class="layui-btn layui-btn-xs" lay-event="code"><i class="layui-icon layui-icon-ok"></i>生成代码</a>
</script>

<script type="text/html" id="previewCode">
  <div class="layui-tab">
    <ul id="previewUl" class="layui-tab-title">

    </ul>
    <div id="previewContent" class="layui-tab-content">

    </div>
  </div>
</script>

<!-- js部分 -->
<script src="/static/libs/highlight/highlight.min.js"></script>
<script>
  layui.use(['layer', 'form', 'table', 'util', 'admin','formX','element'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var util = layui.util;
    var element = layui.element;
    var formX = layui.formX;
    var admin = layui.admin;
    var insOrgSel;
    var permissionType = [],orgs = [];

    /*字典获取接口*/
    admin.req('/sys/sysDictData/get',{type:'permissionType'},function (d) {
      if (0 === d.code) {

      }
    })
    /*部门获取接口*/
    admin.req('/sys/sysOrg/selectData',{},function (d){if(0 === d.code){orgs = d.data;}})

    /* 渲染表格 */
    var insTb = table.render({
      elem: '#dataTable',
      url: '/gen/genTable/list',
      page: true,
      toolbar: ['<p>',
        '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
        '<button lay-event="import" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon layui-icon-upload"></i>导入</button>&nbsp;',
        '<button lay-event="batchGen" class="layui-btn layui-btn-sm icon-btn layui-btn-normal"><i class="layui-icon layui-icon-export"></i>批量生成</button>&nbsp;',
        '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
        '</p>'].join(''),
      cellMinWidth: 100,
      cols: [[
        {type: 'checkbox'},
        {type: 'numbers'},
        {field: 'tableName', title: '表名', sort: true},
        {field: 'tableComment', title: '表描述', sort: true},
        {field: 'className', title: '实体类名称'},
        {field: 'remark', title: '备注'},
        {
          field: 'createTime', title: '创建时间', sort: true
        },
        {
          field: 'updateTime', title: '最后更新时间', sort: true
        },
        {title: '操作', toolbar: '#dataTbBar', align: 'center', width: 320, minWidth: 200}
      ]]
    });

    /* 表格搜索 */
    form.on('submit(dataTbSearch)', function (data) {
      insTb.reload({where: data.field, page: {curr: 1}});
      return false;
    });

    /* 表格工具条点击事件 */
    table.on('tool(dataTable)', function (obj) {
      if (obj.event === 'edit') { // 修改
        showEditModel(obj.data);
      } else if (obj.event === 'del') { // 删除
        doDel(obj);
      }else if(obj.event === 'preview'){
        preview(obj.data);
      }else if(obj.event === 'code'){
        code(obj.data);
      }
    });

    /* 表格头工具栏点击事件 */
    table.on('toolbar(dataTable)', function (obj) {
      if (obj.event === 'add') { // 添加
        showEditModel();
      } else if (obj.event === 'del') { // 删除
        var checkRows = table.checkStatus('dataTable');
        if (checkRows.data.length === 0) {
          layer.msg('请选择要删除的数据', {icon: 2});
          return;
        }
        var ids = checkRows.data.map(function (d) {
          return d.tableId;
        });
        doDel({ids: ids});
      }else if(obj.event === 'import'){
        showDbList();
      }
    });

    /*显示db 表搜索*/
    function showDbList() {
      admin.popupRight({
        type: 2,
        title: '导入表结构',
        area:['67%', '94%'],
        content: '/view/module/gen/dbList'
      });
    }

    function showEditModel(mData) {
      admin.popupRight({
        type: 2,
        title: '编辑表结构',
        area:['99%', '94%'],
        data: { tableId: mData.tableId },
        content: '/view/module/gen/edit'
      });
    }

    /* 删除 */
    function doDel(obj) {
      layer.confirm('确定要删除选中数据吗？', {
        skin: 'layui-layer-admin',
        shade: .1
      }, function (i) {
        layer.close(i);
        var loadIndex = layer.load(2);
        console.log(obj)
        admin.req('/gen/genTable/del', {
          id: obj.data ? obj.data.tableId : '',
          ids: obj.ids ? obj.ids.join(',') : ''
        }, function (res) {
          layer.close(loadIndex);
          if (0 === res.code) {
            layer.msg(res.msg, {icon: 1});
            insTb.reload({page: {curr: 1}});
          } else {
            layer.msg(res.msg, {icon: 2});
          }
        });
      });
    }

    /*预览代码*/
    function preview(obj) {
      admin.req('/gen/genTable/preview?tableId=' + obj.tableId,function (d) {
        if(d.code === 0){
          $.each(d.data,function (index, value) {
            var templateName = index.substring(index.lastIndexOf("/") + 1, index.length).replace(/\.vm/g, "");
            if("sql" !== templateName && "tree.html" !== templateName && "sub-domain.java" !== templateName){
              var codeName = templateName.replace(".", "");
              var language = templateName.substring(templateName.lastIndexOf(".") + 1);
              $('#previewUl').append('<li>'+templateName+'</li>');
              var highCode = hljs.highlight(language, value).value;
              var con = "<pre class=\"layui-code\"><a style=\"float:right\" href=\"javascript:copyText('" + codeName + "')\"><i class=\"fa fa-copy\"></i> 复制</a><code id=\"" + codeName + "\">" + highCode + "</code></pre><textarea id=\"t_" + codeName + "\" style='position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;'></textarea><script>function copyText(codeName){var text = document.getElementById(codeName).innerText;var input = document.getElementById(\"t_\"+codeName);input.value = text;input.select();document.execCommand(\"copy\");$.modal.msgSuccess(\"复制成功\");}<\/script>";
              $('#previewContent').append('<div class="layui-tab-item">'+con+'</div>');
            }
          })

          $('#previewUl li').eq(0).addClass('layui-this');
          $('#previewContent div').eq(0).addClass('layui-show');
          element.render('tab');
        }
      })
      admin.popupRight({
        type:1,
        area:['77%', '94%'],
        content:$('#previewCode').html(),
        success: function (layero, dIndex) {


        }
      })
    }

    /*生成代码*/
    function code(obj) {
      layer.confirm('确定要生成'+obj.tableName + '表的代码吗？', {
        skin: 'layui-layer-admin',
        shade: .1
      }, function (i) {
        layer.close(i);
        var loadIndex = layer.load(2);
        let genType = obj.genType;
        if(genType === "0"){
          location.href = '/gen/genTable/download?tableName=' + obj.tableName;
          layer.msg('执行成功,正在生成代码请稍候…', { icon: 1 });
        }else if(genType === "1"){
          admin.req('/gen/genTable/genCode', {
            tableName: obj.tableName
          }, function (res) {
            if (0 === res.code) {
              layer.msg(res.msg, {icon: 1});
              insTb.reload({page: {curr: 1}});
            } else {
              layer.msg(res.msg, {icon: 2});
            }
          });
        }
        layer.close(loadIndex);

      });
    }

  });
</script>
</body>
#end
